package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pojo.Leaverecords;
import com.ktjy.service.ILeaverecordsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-01-07
 */
@Controller
@RequestMapping("/leaverecords")
public class LeaverecordsController{
    Logger logger= LoggerFactory.getLogger(LeaverecordsController.class);

    @Autowired
    ILeaverecordsService iLeaverecordsService;

    @RequestMapping("/page")
    public String page(Leaverecords leaverecords,
                       @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "2") Integer pageSize,
                       Model model){
        try {
            //分页查询用户信息
            //构造分页条件
            Page page=new Page(pageNo,pageSize);

            Page<Leaverecords> pageInfo = iLeaverecordsService.page(page, null);
            model.addAttribute("pageInfo",pageInfo);

            //查询条件回显
            model.addAttribute("condition",leaverecords);
        } catch (Exception e) {
            logger.error("LeaverecordsController 中的 page 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "page";
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @GetMapping("/save")
    public String toSave(Model model){
        return "save";
    }

    @PostMapping("/save")
    public String doSave(Leaverecords leaverecords){
        try {
            boolean ok = iLeaverecordsService.save(leaverecords);
            if(leaverecords.getId()==null){//新增
                ok = iLeaverecordsService.save(leaverecords);
            }else{//修改
                ok = iLeaverecordsService.updateById(leaverecords);
            }
            if(ok){
                //跳转到列表;重定向到分页方法
                return "redirect:/leaverecords/page";
            }else{
                //跳回新增页面
                return "save";
            }
        } catch (Exception e) {
            logger.error("LeaverecordsController 中的 save 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model){
        try {
            Leaverecords leaverecords = iLeaverecordsService.getById(id);
            model.addAttribute("leaverecords",leaverecords);
        } catch (Exception e) {
            logger.error("LeaverecordsController 中的 view 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "view";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id,Model model){
        try {
            Leaverecords leaverecords = iLeaverecordsService.getById(id);
            model.addAttribute("leaverecords",leaverecords);
        } catch (Exception e) {
            logger.error("LeaverecordsController 中的 toEdit 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }

        return "save";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Long id){
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok = iLeaverecordsService.removeById(id);  //调用service的方法进行数据删除
            if(ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else{
                map.put("code",500);
                map.put("msg","删除失败");
            }
        } catch (Exception e) {
            logger.error("LeaverecordsController 中的 del 方法执行发生错误。{} ",e);
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }
//
//    @GetMapping("/accountExist")
//    @ResponseBody
//    public Object accountExist(@RequestParam("name") String name){
//        Map<String,Object> map=new HashMap<>();
//        try {
//            Leaverecords leaverecords = iLeaverecordsService.findByName(name);
//            if(leaverecords!=null){
//                map.put("exist",1);
//            }else{
//                map.put("exist",0);
//            }
//        } catch (Exception e) {
//            logger.error("SysUserController 中的 accountExist 方法执行发生错误。{} ",e);
//            map.put("exist",-1);
//            map.put("error","系统繁忙，请稍后再试");
//        }
//        return map;
//    }

}
